//
//  ProfileUpdateViewController.m
//  PlayBall
//
//  Created by Edward Zhang on 12-5-7.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "JddProfileUpdateViewController.h"
#import "JddViewControllerManager.h"

@interface JddProfileUpdateViewController ()

@end

@implementation JddProfileUpdateViewController

@synthesize userNameLabel;
//@synthesize realNameTextField;
@synthesize label1;
@synthesize label2;
@synthesize label3;
@synthesize label7;
@synthesize label4;
@synthesize label5;
@synthesize label6;
@synthesize label8;
@synthesize label9;
//@synthesize idcardTextField;
//@synthesize mobilTextField;
@synthesize realNameLabel;
@synthesize idcardLabel;
@synthesize mobilLabel;
@synthesize updata;
@synthesize reBindMobile;
@synthesize updatacommit;

SHARED_VIEW_CONTROLLER_IMPLEMENT(JddProfileUpdateViewController);

- (void)UpdateUserBaseInfoOperationResult:(NSData *)result{
    if (result) {
        NSString *tip = [JddDataParser parseBindMobileResult:result];
        if (tip.length == 0) {
            [JddCommonFunction messageBoxWithMessage:[JddError sharedInstance].message];
            }
            else{
                [JddCommonFunction messageBoxWithMessage:tip];
                JddQueryUserInfoOperation *operation =  [[JddQueryUserInfoOperation alloc] init];
                operation.delegate =self;
                [[JddGlobalParameter sharedInstance].operationQueue addOperation:operation];
            }
    }else{
        [JddCommonFunction messageBoxWithMessage:[JddError sharedInstance].message];
    }
}

-(void)QueryUserInfoOperationResult:(NSData *)result{
    if (result) {
        if ([JddDataParser parseUserInfoResult:result]) {
            [self reloadView];
        }
    }
}

-(void)updateInfo:(id)sender{
    
    if ([[JddUserProfile sharedInstance].IDCard length]<6) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                        message:@"您的个人信息不完整，请先完善个人信息"
                                                       delegate:self
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:@"取消",nil ];
        [alert show];
    }else if (!([[JddUserProfile sharedInstance].bindMobile length]>0&&[[JddUserProfile sharedInstance].IsMobileBind intValue] == 1)){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                        message:@"您的手机号码还未绑定，请先绑定手机号码"
                                                       delegate:self
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:@"取消",nil ];
        [alert show];
    }else{
        [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_BIND_CARD_CARD_INFO
                                                             fromController:self
                                                                 withObject:nil
                                                                withObject2:nil];
        
    }
    
//    if ([[UserProfile sharedInstance].IDCard length]<2) {
//        [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_BIND_CARD_USER_INFO 
//                                                             fromController:self 
//                                                                 withObject:nil 
//                                                                withObject2:nil];
//    }else {
//        [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_BIND_CARD_CARD_INFO 
//                                                             fromController:self 
//                                                                 withObject:nil 
//                                                                withObject2:nil];
//    }
    
}

-(IBAction)reBindMobileSelect:(id)sender{
    if ([[JddUserProfile sharedInstance].IDCard length]<6) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                        message:@"您的个人信息不完整，请先完善个人信息"
                                                       delegate:self
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:@"取消",nil ];
        [alert show];
    }else{
        if ([[JddUserProfile sharedInstance].bindMobile length]<6) {
            [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_RE_BIND_MOBILE
                                                                 fromController:self
                                                                     withObject:nil
                                                                    withObject2:nil];
        }else {
            [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_BIND_MOBILE
                                                                 fromController:self
                                                                     withObject:nil
                                                                    withObject2:nil];
        }
    }
}

- (IBAction)modifyButtonClicked:(id)sender{
    [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_BIND_USER fromController:self withObject:nil withObject2:nil];
    
    
//    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"^([0-9]{17})([0,1,2,3,4,5,6,7,8,9,x,X])$"];
//    NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"^([0-9]{15})$"];
//    if (![idcardTextField.text length]) {
//        [JddCommonFunction messageBoxWithMessage:请输入正确的身份证号！];
//        return;
//    }else {
//        if ([idcardTextField.text length]==18){
//            if (![pred evaluateWithObject:idcardTextField.text]) {
//                [JddCommonFunction messageBoxWithMessage:请输入正确的身份证号！];
//                return;
//            }
//        }
//        if ([idcardTextField.text length]==15) {
//            if (![pred2 evaluateWithObject:idcardTextField.text]) {
//                [JddCommonFunction messageBoxWithMessage:请输入正确的身份证号！];
//                return;
//            }
//        }
//    }
//    if ((![realNameTextField.text length]&&![realNameLabel.text length]) ||
//        (![idcardTextField.text length]&&![idcardLabel.text length] )) {
//        [JddCommonFunction messageBoxWithMessage:INFORMATION_NOT_INTACT];
//        
//        return;
//    }
//    
//    UpdateUserBaseInfoOperation *operation = [[UpdateUserBaseInfoOperation alloc] init];
//    operation.delegate = self;
//    if ([[UserProfile sharedInstance].realName length]) {
//        operation.realName =[UserProfile sharedInstance].realName;
//    }else {
//        operation.realName =realNameTextField.text;
//    }
//    if ([[UserProfile sharedInstance].IDCard length]) {
//        operation.idcard =[UserProfile sharedInstance].IDCard;
//    }else {
//         operation.idcard = idcardTextField.text;
//    }
//    if ([[UserProfile sharedInstance].bindMobile length]) {
//        operation.mobile =[UserProfile sharedInstance].bindMobile;
//    }else {
//        operation.mobile = mobilTextField.text;
//    }
//    [[JddGlobalParameter sharedInstance].operationQueue addOperation:operation];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
  //  [self.navigationItem setTitle:PAGE_USER_PROFILE];
    
        UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];
        customLab.backgroundColor = [UIColor clearColor];
        [customLab setText:PAGE_USER_PROFILE];
        customLab.textAlignment = NSTextAlignmentCenter;

        [customLab setTextColor:[UIColor whiteColor]];
        customLab.font = [UIFont systemFontOfSize:19];
        self.navigationItem.titleView = customLab;
        
        //self.navigationItem.hidesBackButton = YES;

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

//-(IBAction)editOver:(id)sender{
//    [realNameTextField resignFirstResponder];
//    [idcardTextField resignFirstResponder];
//    [mobilTextField resignFirstResponder];
//}

-(void)reloadView{
    updatacommit.hidden = NO;
    reBindMobile.hidden = NO;
    mobilLabel.text = @"";
    label9.text = @"";
    label6.text = @"";
    label5.text = @"";
    label4.text = @"";
    label7.text = @"(未绑定)";
    label8.text = @"立即绑定手机号";
    realNameLabel.text = @"";
    idcardLabel.text = @"";
    updata.hidden = YES;
    label9.hidden = YES;
    label7.hidden = YES;
//    label6.hidden = YES;
    label5.hidden = YES;
    label4.hidden = YES;
//    realNameTextField.hidden = NO;
//    idcardTextField.hidden = NO;
//    realNameTextField.text = @"";
//    idcardTextField.text = @"";
//    if ([[UserProfile sharedInstance].IDCard length]>6) {
//        realNameTextField.hidden = YES;
//        idcardTextField.hidden = YES;
//        idcardLabel.hidden = NO;
//        realNameLabel.hidden = NO;
//        updatacommit.hidden =YES;
  
    NSMutableString *temStr = [NSMutableString stringWithFormat:@"%@",[JddUserProfile sharedInstance].realName];
    if ([temStr length]>=2) {
            NSMutableString *str = [[NSMutableString alloc] init];
            for (int i=0; i<[temStr length]-1; i++) {
                [str appendString:@"*"];
            }
            [temStr replaceCharactersInRange:NSMakeRange(1, [temStr length]-1) withString:str];
        
//        [temStr replaceCharactersInRange:NSMakeRange(1, 1) withString:@"*"];
        realNameLabel.text = temStr;
    }else{
        realNameLabel.text = [JddUserProfile sharedInstance].realName;
    }
   
    
//    }
    if ([[JddUserProfile sharedInstance].bindMobile length] > 8) {
        reBindMobile.hidden = NO;
        label7.hidden = NO;
        if ([[JddUserProfile sharedInstance].IsMobileBind intValue] == 0) {
            label7.text = @"(未绑定)";
            label8.text = @"立即绑定手机";
        } else {
            label7.text = @"(已绑定)";
            label8.text = @"重新绑定手机";
        }
        mobilLabel.text = [JddUserProfile sharedInstance].bindMobile;
    }
    if ([[JddUserProfile sharedInstance].BankCardNumber length] > 8) {
        label1.hidden = NO;
        label2.hidden = NO;
        label3.hidden = NO;
        label4.hidden = NO;
        label5.hidden = NO;
        label6.hidden = NO;
        updata.hidden = YES;
        label4.text = [JddUserProfile sharedInstance].BankCardNumber;
        label5.text = [JddUserProfile sharedInstance].BankName;
    } else {
        label4.text = @"无";
        label5.text = @"无";
        updata.hidden = NO;
    }   
    
    if ([[JddUserProfile sharedInstance].IDCard length] > 6) {
        updatacommit.hidden = YES;
   
        NSMutableString *tempIdStr = [NSMutableString stringWithFormat:@"%@",[JddUserProfile sharedInstance].IDCard];
        if ([tempIdStr length]>13) {
            [tempIdStr replaceCharactersInRange:NSMakeRange(8, 5) withString:@"*****"];
            idcardLabel.text = tempIdStr;
        } else {
            idcardLabel.text = [JddUserProfile sharedInstance].IDCard;
        }
    } else {
        updatacommit.hidden =  NO;
    }
    label6.text = [NSString stringWithFormat:@"%@彩豆",[JddUserProfile sharedInstance].balance];
    userNameLabel.text = [JddUserProfile sharedInstance].name;
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"ShowUserInfo"];
    [MobClick event:@"UserCenter-userinfo"];

    [self.inputView reloadInputViews];
    [self reloadView];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"ShowUserInfo"];

    [self reloadView];
    JddQueryUserInfoOperation *operation =  [[JddQueryUserInfoOperation alloc] init];
    operation.delegate =self;
    [[JddGlobalParameter sharedInstance].operationQueue addOperation:operation];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
